const express = require('express')
const { all } = require('proxy-addr')

const app = express()

app.use(express.static('public'))

app.get('/index', (req, res) => {
    console.log(req.method)
    console.log(req.url)
    console.log(req.httpVersion)
    console.log(req.headers['host'])

    const {a, b} = req.query
    console.log('query参数', a, b)

    res.send('get 返回数据')


})

const allUsers = [
    {id: 1, name: 'tom', age: 12},
    {id: 2, name: 'tom2', age: 13},
    {id: 3, name: 'tom3', age: 14},
]

app.get('/user/:id', (req, res) => {
    const {id} = req.params

    const user = allUsers.find(user => user.id === +id)

    res.send(user)
})

// 注册路由 (post请求)
app.post('/index', (req, res) => {
    console.log('处理 /index post请求')
  
    // body参数
    console.log('body参数', req.body)  // 需要使用到后面一个中间件来处理才可用
  
    res.send('post 返回数据')
  })
  
  
  // 启动服务, 监听指定端口
  app.listen('3000',  () => {
    console.log('服务已启动: http://localhost:3000')
  })
  // http://localhost:3000/index